package vt.ece4564.phonetracker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;


public class SaveRuleActivity extends Activity implements OnClickListener {
	EditText newRuleName;
	
	TimePicker startTime;
	TimePicker endTime;
	
	CheckBox sun, mon, tues, wed, thurs, fri, sat;
	
	Button saveButton;
	
    String radius;
    String longitude;
    String latitude;
    String allrules = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_save_rule_activity);
	    startTime = (TimePicker) findViewById(R.id.timePicker1);
	    endTime = (TimePicker) findViewById(R.id.timePicker2);
	    
	    newRuleName = (EditText) findViewById(R.id.textNewRuleName);
	    
	    Intent intent = getIntent();
	    radius = intent.getStringExtra("rad");
	    longitude = intent.getStringExtra("long");
	    latitude = intent.getStringExtra("lat");
	    allrules = intent.getStringExtra("allrules");
	    
	    TextView tv_rad = (TextView)findViewById(R.id.textDispRadius);
	    TextView tv_lon = (TextView)findViewById(R.id.textDispLong);
	    TextView tv_lat = (TextView)findViewById(R.id.textDispLat);
	    
	    tv_rad.setText("Radius:  \t\t"+radius+" meters");
	    tv_lon.setText("Longitude: \t"+((double) Integer.parseInt(longitude)/1e6));
	    tv_lat.setText("Latitude:  \t"+((double) Integer.parseInt(latitude)/1e6));
	    
	    sun   = (CheckBox)findViewById(R.id.radio0);
	    mon   = (CheckBox)findViewById(R.id.radio1);
	    tues  = (CheckBox)findViewById(R.id.radio2);
	    wed   = (CheckBox)findViewById(R.id.radio3);
	    thurs = (CheckBox)findViewById(R.id.radio4);
	    fri   = (CheckBox)findViewById(R.id.radio5);
	    sat   = (CheckBox)findViewById(R.id.radio6);
	    		
	    saveButton = (Button)findViewById(R.id.saveRuleButton);
	    saveButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//System.out.println(radius + "\t%\t" + latitude + "\t%\t" + longitude);
		String rule = newRuleName.getText().toString() + "%";
		rule += ""+ radius.substring(0, radius.indexOf('.')) + "%" + latitude + "%" + longitude + "%";
		
		rule+=startTime.getCurrentHour()+":"+startTime.getCurrentMinute()+"%";
		rule+=endTime.getCurrentHour()+":"+endTime.getCurrentMinute()+"%";
		
		if(sun.isChecked()){
			System.out.print("Sun\t");
			rule = rule + "Sun-";
		}
		if(mon.isChecked()){
			System.out.print("Mon\t");
			rule = rule + "Mon-";
		}
		if(tues.isChecked()){
			System.out.print("Tue\t");
			rule = rule + "Tue-";
		}
		if(wed.isChecked()){
			System.out.print("Wed\t");
			rule = rule + "Wed-";
		}
		if(thurs.isChecked()){
			System.out.print("Thu\t");
			rule = rule + "Thu-";
		}
		if(fri.isChecked()){
			System.out.print("Fri\t");
			rule = rule + "Fri-";
		}
		if(sat.isChecked()){
			System.out.print("Sat\t");
			rule = rule + "Sat-";
		}
		System.out.println();
		
		System.out.println(rule);
		if(rule.charAt(rule.length()-1) == '-'){
			rule = rule.substring(0, rule.length()-1);
		}
		System.out.println(rule);
		
		Intent intent = new Intent(this, HelloGoogleMaps.class);
		//intent.putExtra("rule", rule);
		if(allrules.length()>0){
			allrules = allrules+"&";
		}
		intent.putExtra("allrules", allrules+rule);
		startActivity(intent);
	}

}
